<%@page import="java.util.logging.Level"%>
<%@page import="java.io.*"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%-- 
    Document   : validate_login
    Created on : Jun 24, 2011, 9:38:09 AM
    Author     : Hung
--%>
<%@include file="include_header.jsp"%>

<% //build the Manager classes if they do not exist
UserManager userData = (UserManager)getServletContext().getAttribute("userData");
CourseManager courseData = (CourseManager)getServletContext().getAttribute("courseData");
LabManager labData = (LabManager)getServletContext().getAttribute("labData");
TeamManager teamData = (TeamManager)getServletContext().getAttribute("teamData");


if(userData == null)
{
    userData = new UserManager();
    //only load the manager arrays on application startup,
    //when the server first starts up
    userData.getFromFile("userData.ser");
    getServletContext().setAttribute("userData", userData);
    
}
if(courseData == null)
{
    courseData = new CourseManager();
    //Log.getInstance().info("course was null");
    courseData.getFromFile("courseData.ser");
    getServletContext().setAttribute("courseData", courseData);
    
}
if(labData == null)
{
    labData = new LabManager();
    labData.getFromFile("labData.ser");
    getServletContext().setAttribute("labData", labData);
    
    
}
if(teamData == null)
{
    teamData = new TeamManager();
    teamData.getFromFile("teamData.ser");
    getServletContext().setAttribute("teamData", teamData);
    
    
}
 //load saved serialization files  
    
  //userData.getFromFile("userData.ser");
  //courseData.getFromFile("courseData.ser");
  //teamData.getFromFile("teamData.ser");
  //labData.getFromFile("labData.ser");
  
  
  int totalUsers = 0, totalTeams = 0, totalCourses = 0, totalLabs = 0;
  
  try
  {
      File file = new File("statics.txt");
      BufferedReader input = new BufferedReader(new FileReader(file));

      String totalUsersStr = input.readLine();
      String totalTeamsStr = input.readLine();
      String totalCoursesStr = input.readLine();
      String totalLabsStr = input.readLine();

      if(totalUsersStr != null)
      {
        totalUsers = Integer.parseInt(totalUsersStr);
      }
      if(totalTeamsStr != null)
      {
        totalTeams = Integer.parseInt(totalTeamsStr);
      }
      if(totalCoursesStr != null)
      {
        totalCourses = Integer.parseInt(totalCoursesStr);
      }
      if(totalLabsStr != null)
      {
        totalLabs = Integer.parseInt(totalLabsStr);
      }
      input.close();
  } catch (Exception e)
  {
        File file = new File("statics.txt");
        BufferedWriter output = new BufferedWriter(new FileWriter(file));
        output.write(User.getTotalUsers() + "");
        output.write("\n");
        output.write(Team.getTotalTeams()+ "");
        output.write("\n");
        output.write(Course.getTotalCourses()+ "");
        output.write("\n");
        output.write(Lab.getTotalLabs()+ "");
        output.write("\n");
        output.close();
  }
  
  User.setTotalUsers(totalUsers);
  Team.setTotalTeams(totalTeams);
  Course.setTotalCourses(totalCourses);
  Lab.setTotalLabs(totalLabs);
  

  
 //userData = userData.getFromFile("userData.ser");
 //courseData = courseData.getFromFile("courseData.ser");
 //teamData = teamData.getFromFile("teamData.ser");
 //labData = labData.getFromFile("labData.ser");
    
//get the user provided credentials and check them against the user
//manager, send the user home if correct, back to index with error otherwise
String userName=request.getParameter("userName");
String password=request.getParameter("pwd");


Authenticator auth = new Authenticator();
boolean loggedIn = auth.authenticateUser(userName, password, userData);
if(loggedIn)
{
    
    User aUser = auth.getUser(userName, password, userData);
    session.setAttribute("persistUser", aUser);
    aUser = (User)session.getAttribute("persistUser");
    if(aUser instanceof SystemAdmin)
    {
        response.sendRedirect("admin_home.jsp");
    }
    else if(aUser instanceof Instructor)
    {
        response.sendRedirect("instr_home.jsp");
    }
    else if(aUser instanceof Student)
    {
        response.sendRedirect("stude_home.jsp");
    }
}
else
{
    //Log.getInstance().log(Level.SEVERE, "The user " + userName + " failed to log in.");
    response.sendRedirect("index.jsp?errCode=invalid");
}
%>
